using System;
using System.Runtime.InteropServices;

namespace Sturm
{
    public struct Coord
    {
        public short X;
        public short Y;
    }

    public struct SmallRectangle
    {
        public short Left;
        public short Top;
        public short Right;
        public short Bottom;
    }

    public struct ConsoleInfo
    {
        public Coord Size;
        public Coord CurrentPosition;
        public short Attributes;
        public SmallRectangle Window;
        public Coord MaximumWindowSize;
    }

    /// <summary>
    /// Class for console color manipulation
    /// </summary>
    public static class ConsoleColor
    {
        /// <summary>
        /// Possible color values
        /// </summary>
        /// <remarks>
        /// In order to achieve bright colors use or the color with Color.White
        /// </remarks>
        [Flags]
        public enum Color
        {
            /// <summary>
            /// Black color
            /// </summary>
            Black = 0x0000,

            /// <summary>
            /// Blue color
            /// </summary>
            Blue = 0x0001,

            /// <summary>
            /// Green color
            /// </summary>
            Green = 0x0002,

            /// <summary>
            /// Cyan color
            /// </summary>
            Cyan = 0x0003,

            /// <summary>
            /// Red color
            /// </summary>
            Red = 0x0004,

            /// <summary>
            /// Magenta color
            /// </summary>
            Magenta = 0x0005,

            /// <summary>
            /// Yellow color
            /// </summary>
            Yellow = 0x0006,

            /// <summary>
            /// Grey color
            /// </summary>
            Grey = 0x0007,

            /// <summary>
            /// White color
            /// </summary>
            White = 0x0008
        }

        [DllImport("Kernel32.dll")]
        private static extern bool SetConsoleTextAttribute(IntPtr consoleOutput, int attributes);

        [DllImport("Kernel32.dll")]
        private static extern int GetConsoleScreenBufferInfo(IntPtr consoleOutput, out ConsoleInfo consoleInfo);

        [DllImport("Kernel32.dll")]
        private static extern IntPtr GetStdHandle(int handle);

        private const int StandardOutputHandle = -11;

        /// <summary>
        /// Main program entry point
        /// </summary>
        public static int Main(string[] args)
        {
            Color color;
            int colorIndex = 0;

            try
            {
                if (args[0].ToUpperInvariant() == "BRIGHT")
                {
                    colorIndex = 1;
                }

                int i;
                if (Int32.TryParse(args[colorIndex], out i) == true)
                {
                    ShowSyntax();
                    return -1;
                }

                color = (Color)Enum.Parse(typeof(Color), args[colorIndex], true);

                if (colorIndex == 1)
                {
                    color |= Color.White;
                }
            }
            catch
            {
                ShowSyntax();
                return -1;
            }

            IntPtr consoleHandle = ConsoleColor.GetStdHandle(ConsoleColor.StandardOutputHandle);

            // Get the current color information
            ConsoleInfo consoleInfo;
            ConsoleColor.GetConsoleScreenBufferInfo(consoleHandle, out consoleInfo);

            ConsoleColor.SetConsoleTextAttribute(consoleHandle, (int)color);
            for (int i = 1 + colorIndex; i < args.Length; i++)
            {
                Console.Write(args[i]);
                Console.Write(" ");
            }
            Console.WriteLine();

            // Set the color back to the original color
            ConsoleColor.SetConsoleTextAttribute(consoleHandle, consoleInfo.Attributes);

            return 0;
        }

        private static void ShowSyntax()
        {
            Console.WriteLine("cecho syntax:");
            Console.WriteLine(" cecho.exe [BRIGHT] COLOR TEXT");
            Console.WriteLine();
            Console.WriteLine(" Bright indicates a bright color");
            Console.WriteLine(" Possible values for COLOR:");

            string[] colors = Enum.GetNames(typeof(Color));
            foreach (string color in colors)
            {
                Console.WriteLine("   " + color);
            }
        }
    }
}
